﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using System.Configuration;
using LinkCard.CustomerView.Common;

namespace LinkCard.CustomerView
{
    public partial class ProductCategory : System.Web.UI.Page
    {
        private int PageSize = 32;
        private int CurrentPage = 1;
        private int TotalPageCount = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Quyền lợi khách hàng - LinkCard";
            if (IsPostBack)
                return;

            long catid = 0;
            int pagesize = 9;
            string _pz = ConfigurationManager.AppSettings["ProductCategoryPageSize"];
            if (!string.IsNullOrEmpty(_pz))
            {
                pagesize = int.Parse(_pz);
            }
            string _page = Request.Params["page"];
            if (!string.IsNullOrEmpty(_page))
                CurrentPage = Convert.ToInt32(_page);

            string _catid = Page.RouteData.Values["pcid"].ToString();
            if (!string.IsNullOrEmpty(_catid))
            {
                catid = long.Parse(_catid);

                var category=new CategoryDao().GetById(catid);
                catname.Text = HttpUtility.HtmlDecode(category.Name);

                List<ShortDeal> toplatest=new List<ShortDeal>();
                List<ShortDeal> source;
                int slidecount = 5;
                int totalrecord = new DealDao().GetTotalRecord(catid);
                if(totalrecord>slidecount)
                {
                    totalrecord -= slidecount;
                }
                if (CurrentPage == 1)
                {
                    pagesize = pagesize + slidecount;
                    source = new DealDao().GetLatestDealByCategory(catid, CurrentPage, pagesize, 0);
                    toplatest = source.Take(slidecount).ToList();
                    if (source.Count >= slidecount)
                    {
                        source.RemoveRange(0, slidecount);
                    }
                    else
                    {
                        source.Clear();
                    }
                }
                else
                {
                    source = new DealDao().GetLatestDealByCategory(catid, CurrentPage, pagesize, slidecount);
                    toplatest = new DealDao().GetLatestDealByCategory(catid, slidecount);
                }

                foreach (ShortDeal item in source)
                {
                    item.ParentCategoryId = catid;
                }
                foreach (ShortDeal item in toplatest)
                {
                    item.ParentCategoryId = catid;
                }

                slide.Deals = toplatest;

                if(totalrecord>0)
                {
                    TotalPageCount = totalrecord%pagesize > 0 ? totalrecord/pagesize + 1 : totalrecord/pagesize;
                }
                else
                {
                    TotalPageCount = 0;
                }

                ltrPaging.Text = Pager.Create(CurrentPage, TotalPageCount);

                rpcatproduct.DataSource = source;
                rpcatproduct.DataBind();
            }
        }
    }
}